//
//  UUIDTool.m
//
//
//  Created by 孟瑞瑞 on 16/6/20.
//  Copyright © 2016年 milan. All rights reserved.
//

#import "UUIDTool.h"
#import "KeyChainStore.h"

@implementation UUIDTool

static NSString *unpwd = @"com.company.app.usernamepassword";
static NSString *errorstr = @"CSDN-UUID-ERROR-STRING";

+(NSString *)getUUID
{
    NSString * strUUID = (NSString *)[KeyChainStore load:unpwd];
    
    //首次执行该方法时，uuid为空
    if (![strUUID isKindOfClass:[NSString class]]||[strUUID isEqualToString:@""] || !strUUID)
    {
         strUUID = [self uuidString];
        
        if (![strUUID isKindOfClass:[NSString class]]) {
            strUUID = errorstr;
        }
        strUUID = [strUUID capitalizedString];
        //将该uuid保存到keychain
        [KeyChainStore save:unpwd data:strUUID];

    }
    return strUUID;
}


+ (NSString *)uuidString
{
    CFUUIDRef uuid_ref = CFUUIDCreate(NULL);
    CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref);
    NSString *uuid = [NSString stringWithString:(__bridge NSString *)uuid_string_ref];
    CFRelease(uuid_ref);
    CFRelease(uuid_string_ref);
    return [uuid lowercaseString];
}

@end
